//
//  BillViewController.m
//  AAIM
//
//  Created by 远的小布丁 on 2020/1/26.
//  Copyright © 2020 AAIM. All rights reserved.
//

#import "BillViewController.h"
#import "BillTableViewCell.h"
#import "DataTool.h"
#import "BillModel.h"
#import "BillContentModel.h"
#import "PublicContentViewController.h"
@interface BillViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView * billTableView;
@property (nonatomic,strong)NSMutableArray * dataAry;
@end

@implementation BillViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navTitle = @"支付凭证";
    [self.view addSubview:self.billTableView];
    [self saveOneBill];
    [self getBillListData];
}

- (void)saveOneBill{
//    NSArray * ary = @[@{@"k":@"收益类型",@"v":@"服务号收益"},@{@"k":@"订单编号",@"v":@"FWH210209109398293039842"},@{@"k":@"获得DST",@"v":@"10"},@{@"k":@"订单时间",@"v":@"1月11日13时31分20秒"}];
//    NSString * json = [DataTool convertToJSONData:ary];
//
//    [RequestData PostBillListWithCreateTime:@"2020-01-25 22:37:50" userDescribe:@"测试账单" withFuuid:[LoginManager shareLoginManager].userModel.fuuid withOrderid:@"2020012622385078901" withParameter:json withRemark:@"不错" withTitle:@"我来测试下" withUrl:@"www.baidu.com" Success:^(NSDictionary * _Nonnull Dict) {
//        NSLog(@"创建数据：%@",Dict);
//        
//    } failure:^(NSString * _Nonnull errMsg) {
//        NSLog(@"出错%@",errMsg);
//    }];
}


- (void)getBillListData{
    KWeakSelf;
    [RequestData PostBillListWithToken:[LoginManager shareLoginManager].userModel.token userUUid:[LoginManager shareLoginManager].userModel.uuid withPage:1 withUdid:[GetUUID getUUID] Success:^(NSDictionary * _Nonnull Dict) {
        NSLog(@"我的账单：%@",Dict);
//        &&![CommonTools isNull:Dict key:@"data"]
        if ([Dict[@"code"] integerValue] == 200 ) {
            for (NSDictionary * dic in Dict[@"data"]) {
                BillModel * model = [BillModel mj_objectWithKeyValues:dic];
                [weakSelf.dataAry addObject:model];
            }
            [weakSelf.billTableView reloadData];
        }
    } failure:^(NSString * _Nonnull errMsg) {
        [self showTipHudWithText:errMsg];
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataAry.count;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BillTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"BillTableViewCell"];
    if (!cell) {
        cell = [[BillTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BillTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = _dataAry[indexPath.section];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    BillModel * model =_dataAry[indexPath.section];
    NSInteger num = model.parameterJson.count;
    CGFloat height = 16+21+9+17+14+18+14*num+14*num+17+14+46;
    return height;
//    return 247.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BillModel * model = _dataAry[indexPath.section];
    PublicContentViewController * webView = [[PublicContentViewController alloc]init];
    webView.billUrl = model.url;
    webView.pageType = 1;
    [self.navigationController pushViewController:webView animated:YES];
}

- (UITableView *)billTableView{
    if (!_billTableView) {
        _billTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.gk_navigationBar.bottom, kScreenWidth, kScreenHeight-self.gk_navigationBar.bottom) style:UITableViewStyleGrouped];
        _billTableView.delegate = self;
        _billTableView.dataSource = self;
        _billTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _billTableView.tableHeaderView = [UIView new];
        _billTableView.tableFooterView = [UIView new];
    }
    return _billTableView;
}

- (NSMutableArray *)dataAry{
    if (!_dataAry) {
        _dataAry = [NSMutableArray array];
    }
    return _dataAry;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
